import { createStore } from 'vuex'
import { menusList, tablelist } from "@/http/api.js"
import persist from 'vuex-persistedstate'
export default createStore({
  state: {
    userInfo: null,
    menuList: [],
    items: [],
    arr: [],
    charttime: [],
    obj: {
      timeStr: [],
      income: [],
      expense: [],
      balance: [],
    },
    table:{},
    bing:[],
    dailyCurve:[]
  },
  getters: {
    getMenuList(state) {
      return state.menuList
    }
  },
  mutations: {
    setUserInfo(state, data) {
      console.log('vuex', data);
      state.userInfo = 'Bearer ' + data.token
    },

    setSignOut(state) {
      state.userInfo = null
      state.menuList = []
      state.buttonList = []
    },

    setMenuList(state, data) {
      // console.log(data);
      if (data.length == 0) {
        state.menuList = null
      } else {
        state.menuList = data
      }
    },
    items(state, data) {
      let obj = {
        title: data
      }
      state.arr.push(obj)
      for (var i = 0; i < state.arr.length; i++) {
        for (var j = i + 1; j < state.arr.length; j++) {
          if (state.arr[i].title == state.arr[j].title) {
            state.arr.splice(j, 1)
          }
        }
      }
      state.items = state.arr
    },
    del(state, index) {
      state.items.splice(index, 1)
    },
    right(state, i) {
      state.items = state.arr.slice(0, i + 1)
      console.log(i);
    },
    //反向数据图
    charttime(state, data) {
        state.obj.timeStr=[]
        state.obj.income=[]
        state.obj.expense=[]
        state.obj.balance=[]
      data.forEach(element => {
        state.obj.timeStr.push(element.timeStr)
        state.obj.income.push(element.income)
        state.obj.expense.push(element.expense)
        state.obj.balance.push(element.balance)
      });
      // console.log('vuex',state.obj);
    },
    //表格数据
    tablelist(state,data){
      state.table=data
        // console.log('vuex',data);
    },
    bing(state,data){
        state.bing=data
        // console.log('vuex',state.bing);
    },
    dailyCurve(state,data){
        state.dailyCurve=data
        console.log('vuex',state.dailyCurve);
    }
  },
  actions: {
    userInfos({ commit }, data) {
      commit("setUserInfo", data)
    },

    signOuts({ commit }) {
      commit("setSignOut")
    },

    getLists({ commit }, data) {
      menusList().then(res => {
        commit("setMenuList", res.data)
      })
    },
  },
  modules: {
  },
  plugins: [
    new persist({
      storage: window.localStorage,
    }),
  ],
})
